package com.xiangwushuo.framework.activity.main.fragment.my

import com.lib.common.base.mvvm.CommonViewModelFragment
import com.lib.common.constant.INTENT_KEY_PARAM1
import com.lib.common.constant.INTENT_KEY_PARAM2
import androidx.databinding.DataBindingUtil
import android.os.Bundle
import com.lib.framework.entity.Event
import com.lib.framework.manager.LogManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.gyf.immersionbar.ImmersionBar

import com.xiangwushuo.framework.R
import com.xiangwushuo.framework.databinding.FragmentMyBinding

/**
 * Copyright (C), 2019-2020
 * FileName: MyFragment
 * Author:
 * Date: 2020-01-01 00:00
 * Description: 我的 fragment
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class MyFragment : CommonViewModelFragment<MyViewModel>() {

    companion object {

        /**
         * 发给 fragment 的事件
         */
        const val EVENT_XXX = 1 // XXX 事件

        /**
         * 发送给 activity 的事件，activtiy 需要实现 SendEventToActivityListener 接口
         */
        const val FRAGMENT_EVENT_XXXX = 1 // XXXX事件

        @JvmStatic
        fun newInstance(param1: String, param2: String) = MyFragment().apply {
            arguments = Bundle().apply {
                putString(INTENT_KEY_PARAM1, param1)
                putString(INTENT_KEY_PARAM2, param2)
            }
        }
    }

    private var mDataBinding: FragmentMyBinding? = null

    override fun getViewModelClass() = MyViewModel::class.java

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        if (mDataBinding == null) {
            mDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_my, container, false)
            mDataBinding?.viewModel = mViewModel
        }
        return mDataBinding?.root
    }

    override fun immersionBarEnabled(): Boolean {
        return true
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        ImmersionBar.with(this)
            .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
            .statusBarDarkFont(true)
            .statusBarColor(R.color.white)
            .init()
    }

    override fun initView() {
        mDataBinding?.let { dataBinding ->

        }
    }

    override fun lazyLoad() {

    }

    override fun dealEvent(event: Event?): Boolean {
        // 处理 viewModel 发送过来的事件
        when (event?.code) {
            EVENT_XXX -> {
                val extraObj = event.extraObj
                if (extraObj is String) {
                    LogManager.i(mTag, "XXX 事件 的额外参数 extraObj = $extraObj")
                }
                return true
            }
        }

        return super.dealEvent(event)
    }
}
